package tcp2;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
	public static void main(String[] args) throws IOException{
		
		final String PATH="download/";
		Socket socket=new Socket("127.0.0.1",8888);
		OutputStream out=socket.getOutputStream();
		InputStream in=socket.getInputStream();
		BufferedReader reader=new BufferedReader(new InputStreamReader(in));
		Scanner scanner=new Scanner(System.in);
		String tip=null;
		String filename=null;
		while(!"OK".equals(tip=reader.readLine())){
			System.out.println(tip);
			filename=scanner.nextLine();
			out.write((filename+"\n").getBytes());
		}
		OutputStream fout=new FileOutputStream(PATH+filename);
		
		byte[] buf=new byte[4];
		int len=0;
		while((len=in.read(buf))!=-1){
			fout.write(buf,0,len);
		}
		fout.close();
		out.close();
		reader.close();
		scanner.close();
		socket.close();
		
	}
}
